package com.zhouheng.springbootactiviti.common.pojo;

import com.zhouheng.springbootactiviti.common.security.GrantedAuthorityImpl;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

public class SecurityUserInfo implements UserDetails {

    public SecurityUserInfo() {
    }

    public SecurityUserInfo(String username, String password, List<String> roles) {
        this.username = username;
        this.password = password;
        this.roles = roles;
    }

    private String username;

    private String password;

    private List<String> roles;

    /**
     * 从数据库中取出roles字符串后，进行分解，构成一个GrantedAuthority的List返回
     *
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return roles.stream().map(GrantedAuthorityImpl::new).collect(Collectors.toSet());
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        // 账号是否未过期
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        // 账号是否未被锁定
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        // 凭据是否未过期
        return true;
    }

    @Override
    public boolean isEnabled() {
        // 是否已启动
        return true;
    }

}
